package com.gdth.utils;

import java.net.URI;
import java.util.ArrayList;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

/**
 * App
 * @author Arvin
 *
 */
public class BaseAppUtil {
	/**
	 * 获取手机型号
	 * @return
	 */
	public static String getModelForOS(){
		return android.os.Build.MODEL;
	}

	/**
	 * 获取android版本
	 * @return
	 */
	public static int getVersionSDKForOS(){
		return android.os.Build.VERSION.SDK_INT;
	}
	
	/**
	 * 获取android版本名称
	 * @return
	 */
	public static String getVersionReleaseForOS(){
		return android.os.Build.VERSION.RELEASE;
	}
	
	public static String getSDCardPath(){
		if (existSDCard())
			return Environment.getExternalStorageDirectory().toString();
		else 
			return null;
	}
	
	public static URI getSDCardUri(){
		if (existSDCard())
			return Environment.getExternalStorageDirectory().toURI();
		else
			return null;
	}
	
	public static boolean existSDCard(){
		return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
	}
	
	/**
	 * Get the App version code
	 * @param context
	 * @return
	 */
	public static int getVerCode(Context mContext) {
		String mPackageName = mContext.getPackageName();
		
        int verCode = -1;  
        try {  
            verCode = mContext.getPackageManager().getPackageInfo(mPackageName, 0).versionCode;  
        } catch (NameNotFoundException e) {  
            e.printStackTrace();  
        }  
        return verCode;  
    }  
	
	/**
	 * Get the App version name
	 * @param context
	 * @return
	 */
    public static String getVerName(Context mContext){
    	String mPackageName = mContext.getPackageName();
    	
        String verName = "";  
        try {  
            verName = mContext.getPackageManager().getPackageInfo(mPackageName, 0).versionName;  
        } catch (NameNotFoundException e) {  
            e.printStackTrace(); 
        }  
        return verName;     
    }
    
    /**
	 * 判断服务是否在运行
	 * @param context
	 * @param serviceName
	 * @return
	 */
	public static boolean isServiceWorked(Context context, String serviceName) {
		ActivityManager myManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager.getRunningServices(Integer.MAX_VALUE);
		for (int i = 0; i < runningService.size(); i++) {
			if (runningService.get(i).service.getClassName().toString().equals(serviceName)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断是否debug状态
	 * @param context
	 * @return
	 */
	public static boolean isApkDebugable(Context context) {  
        try {  
            ApplicationInfo info= context.getApplicationInfo();  
                return (info.flags&ApplicationInfo.FLAG_DEBUGGABLE)!=0;  
        } catch (Exception e) {  
              
        }  
        return false;  
    }
    
    /**
	 * exit event
	 */
	public static void exit(Context mContext, int imageId){  
		new AlertDialog.Builder(mContext)
		.setTitle("提示").setMessage("即将退出登录，是否继续?").setIcon(imageId)
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int whichButton) {
    			System.exit(0);
			}
		})
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				//取消按钮事件
			}
		})
		.show();
    } 
}
